package rolesystem.agentlogic.utilityclasses;

import java.util.ArrayList;

import rolesystem.middle.managers.DataOutputManager;


public class AggregateData 
{
	/**
	 * Data of the agents "living" in the simulation
	 */
	private ArrayList<AgentLog> agentData;
	
	/**
	 * Timestamp of the start time of the simulation
	 */
	private long startTime;
	
	/**
	 * Timestamp of the end time of the simulation
	 */
	private long endTime;
	
	public static final String data_separator=",";
	
	public static final String row_separator=";";
	
	public static final String line_separator=System.getProperty("line.separator");
	
	public AggregateData()
	{
		agentData= new ArrayList<AgentLog>();
		
	}
	
	public void setendTime(long time)
	{
		this.endTime=time;
	}
	
	public void setstartTime(long time)
	{
		this.startTime=time;
	}
	
	public long getStartTime()
	{
		return this.startTime;
	}
	
	public void createagentData(int numberOfAgents)
	{
		for(int agent_id=0;agent_id<numberOfAgents;agent_id++)
		{
			this.agentData.add(agent_id, new AgentLog());
		}
	}
	
	public void clearagentData()
	{	
		this.agentData.clear();	
	}
	
	public void setagentData(int agent_id,AgentLog data)
	{
		try 
		{
			this.agentData.set(agent_id, data);
		} catch (IndexOutOfBoundsException  e) 
		{
			//this.agentData.add(agent_id, data);
			DataOutputManager.printString("ERROR ERROR ERROR");
		}
	}
	
	public String toString()
	{
		String output;
		
		output="Time" + data_separator + String.valueOf(startTime)+ data_separator +String.valueOf(endTime) + row_separator+ line_separator;
		output+= "Agents" + data_separator + String.valueOf(agentData.size()) + row_separator+ line_separator;

		for(int i=0;i<agentData.size();i++)
		{
			output+= "AgentN" + data_separator + String.valueOf(i) + data_separator 
				    + agentData.get(i).getnumberofMovements() + data_separator 
				    + agentData.get(i).getobjectCarried() + data_separator  
				    + agentData.get(i).gettimesAsManager() + data_separator 
				    + agentData.get(i).gettimesAsCollaborativeCarrier() + data_separator 
				    + agentData.get(i).getlostObjects() + data_separator
				    + row_separator+line_separator;
		}	
		
		output+= line_separator;
		
		return output;
	}
	
	public String toStringPeriodic()
	{
		String output="";

		for(int i=0;i<agentData.size();i++)
		{
			output+= "AgentN" + data_separator + String.valueOf(i) + data_separator 
				    + agentData.get(i).getnumberofMovements() + data_separator 
				    + agentData.get(i).getobjectCarried() + data_separator  
				    + agentData.get(i).gettimesAsManager() + data_separator 
				    + agentData.get(i).gettimesAsCollaborativeCarrier() + data_separator 
				    + agentData.get(i).getlostObjects() + data_separator
				    + row_separator + line_separator;
		}	
		
		return output;
	}
	
}
